﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NielsRask.K242.Core;
using NielsRask.K242.Core.AI;
using SdlDotNet.Graphics;
using System.Drawing;

namespace RenderTest1
{
	public class ColonyView
	{
		List<LayerBase> layers;
		Surface screen;
		AIPlayer ai;
		Emperor emperor;
		Sector sector;
		//public ColonyView( Size size )
		//{
		//    layers = new List<LayerBase>();

		//    sector = new Sector( new SizeF( 100, 100 ) );
		//    sector.Populate( 15 );

		//    Asteroid asteroid = sector.Asteroids[ 0 ];
		//    //emperor = new Emperor(sector, asteroid);
		//    sector.Players.Add( new Emperor( sector, asteroid ) );
		//    sector.Players.Add( new Emperor( sector, sector.Asteroids[ 3 ] ) );
		//    ////asteroid.BuildColony();
		//    //ai = new AIPlayer();
		//    //ai.Occupy(asteroid);
		//    //emperor.Colonies.Add(asteroid.Colony);


		//    //asteroid.
		//    var guiLayer = new GuiLayer( size );
		//    layers.Add( new TerrainLayer( size, asteroid ) );
		//    layers.Add( new SelectionLayer( size, asteroid, guiLayer ) );
		//    layers.Add( new BuildingLayer( size, asteroid ) );
		//    layers.Add( guiLayer );
		//    screen = Video.SetVideoMode( size.Width, size.Height, 32, true, false, false );
		//    //screen.Fill(new Rectangle(new Point(0, 0), screen.Size), Color.FromArgb(0, 0, 0));
		//    screen.Fill( new Rectangle( new Point( 0, 0 ), screen.Size ), Color.BlueViolet );
		//}

		public ColonyView( Sector sector, Asteroid asteroid, Size screenSize )
		{
			layers = new List<LayerBase>();

			var guiLayer = new GuiLayer( screenSize );
			layers.Add( new TerrainLayer( screenSize, asteroid ) );
			layers.Add( new SelectionLayer( screenSize, asteroid, guiLayer ) );
			layers.Add( new BuildingLayer( screenSize, asteroid ) );
			layers.Add( guiLayer );
			screen = Video.SetVideoMode( screenSize.Width, screenSize.Height, 32, true, false, false );
			//screen.Fill(new Rectangle(new Point(0, 0), screen.Size), Color.FromArgb(0, 0, 0));
			screen.Fill( new Rectangle( new Point( 0, 0 ), screen.Size ), Color.BlueViolet );

		}


		public void RegisterClick( SdlDotNet.Input.MouseButtonEventArgs e )
		{
			int i = 0;
			while ( i < layers.Count && !layers[ i ].RegisterClick( e ) )
			{
				//System.Console.WriteLine( "forwarding click event to layer " + i );
				i++;
			}
		}
		int counter = 0;
		public void Render()
		{
			//screen.Fill(new Rectangle(new Point(0, 0), screen.Size), Color.BlueViolet);
			for ( int i = 0; i < layers.Count; i++ )
			{
				//Console.WriteLine( "rendering layer " + i );
				screen.Blit( layers[ i ].RenderSurface(), new Point( 0, 0 ) );//,, screen.Rectangle );

			}
			screen.Update();
			//counter++;
			//if (counter >= 30)// grissefy at drive AI updaten herfra - burde nok være fra en tråd
			//{
			//    //ai.Colony.Update();
			//    //ai.Update();
			//    sector.Update();
			//    counter = 0;
			//}
		}

		public void Close()
		{
			screen.Close();
		}

	}


}
